﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Imaginarium.XML
{
    public class XmlHelper
    {
        public static int getIntByTagname(XmlElement element, string tagName)
        {
            int ans = 0;
            if (element != null)
            {
                XmlNodeList intList = element.GetElementsByTagName(tagName);
                if (intList.Count == 1)
                {
                    try
                    {
                        ans = int.Parse(intList[0].InnerText);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
            return ans;
        }

        public static XmlNodeList getXmlNodeListByTagname(XmlElement element, string tagName) {
            XmlNodeList ans = null;
            if (element != null)
            {
                ans = element.GetElementsByTagName(tagName);
            }
            return ans;
        }
        public static string getStringByTagname(XmlElement element, string tagName)
        {
            string ans = null;
            if (element != null)
            {
                XmlNodeList nl = element.GetElementsByTagName(tagName);
                if (nl.Count > 0)
                {
                    ans = element.GetElementsByTagName(tagName)[0].InnerText;
                }
            }
            return ans;
        }
        public static XmlElement getXmlElementByTagname(XmlElement element, string tagName) {
            XmlElement ans= null;
            if (element != null)
            {
                ans = (XmlElement)element.GetElementsByTagName(tagName)[0];
            }
            return ans;
        }

        public static bool getBoolByTagname(XmlElement element, string tagName)
        {
            bool ans = false;
            if (element != null)
            {
                if (getIntByTagname(element, tagName) != 0)
                    ans = true;
            }
            return ans;
        }

    }
}
